/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kylingroup.Desktop.DAO;

import com.kylingroup.Desktop.Bean.Bean_Plugin;
import com.kylingroup.Desktop.Database.DBConnection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author kylin
 */
public class Dao_Plugin implements DAOManagement{
    private DBConnection dbconn=null;
    private Connection conn=null;
    private Statement stmt=null;
    private Statement stmt2=null;
    private PreparedStatement pstmt=null;
    private PreparedStatement pstmt2=null;
    private ResultSet rs=null;
    private ResultSet rs2=null;
    private ResultSetMetaData rsmd=null;

    @Override
    public boolean add(Object object) {
        boolean result = false;
        String sql="insert into tbplugins(id,autoload,name,location,clazz,category,version,description,author,email) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        Bean_Plugin plugin=(Bean_Plugin)object;
        int count=0;
        try {
        	conn.setAutoCommit(false);
        	pstmt = conn.prepareStatement(sql);
    		
    		pstmt.setObject(1,null);
    		pstmt.setString(2, String.valueOf(plugin.isAutoload()));
    		pstmt.setString(3, plugin.getName());
    		pstmt.setString(4, plugin.getLocation());
    		pstmt.setString(5, plugin.getClazz());
    		pstmt.setString(6, plugin.getCategory());
                pstmt.setString(7, plugin.getVersion());
                pstmt.setString(8, plugin.getDescription());
                pstmt.setString(9, plugin.getAuthor());
                pstmt.setString(10, plugin.getEmail());
    
    		count=pstmt.executeUpdate();
    		
    		conn.commit();
    		conn.setAutoCommit(true);
         
            if(count>0){
                result=true;
            }else{
                result=false;
            }
        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            dbconn.closeAll(rs, stmt, pstmt, conn);
        }
        return result;
    }

    @Override
    public boolean delete(Object object) {
        return false;
    }

    @Override
    public boolean update(String key, Object value, String column, Object columnvalue) {
        return false;
    }

    @Override
    public boolean updateAll(Object bean) {
        return false;
    }

    @Override
    public int totleRecords(String key, Object value) {
        return 0;
    }

    @Override
    public Object find(String key, Object value) {
        return null;
    }

    @Override
    public Object findWithAnd(String key1, Object value1, String key2, Object value2) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Object findWithAnd(String key1, Object value1, String key2, Object value2, String key3, Object value3) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public List<Object> findAll() {
        List<Object> list=new ArrayList<Object>();
	String sql="select * from tbplugins order by id desc";
	try{
	    stmt=conn.createStatement();
	    rs=stmt.executeQuery(sql);
	        
	    while(rs.next()){
                Bean_Plugin p=new Bean_Plugin();
                //id,autoload,name,location,clazz,category,version,description,author,email
                p.setId(rs.getInt("id"));
                p.setAutoload(Boolean.parseBoolean(rs.getString("autoload")));
                p.setName(rs.getString("name"));
                p.setLocation(rs.getString("location"));
                p.setClazz(rs.getString("clazz"));
                p.setCategory(rs.getString("category"));
                p.setVersion(rs.getString("version"));
                p.setDescription(rs.getString("description"));
                p.setAuthor(rs.getString("author"));
                p.setEmail(rs.getString("email"));
	        list.add(p);
	    }
	}catch(SQLException e){
	            e.printStackTrace();
		}finally{
			dbconn.closeAll(rs, stmt, pstmt, conn);
		}
		return list;
    }

    @Override
    public List<Object> findAll(String key, Object value) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Object Page(int curPage, int rowsPerPage) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void setConnection(DBConnection dbconn) {
        this.dbconn=dbconn;
        this.conn=this.dbconn.getConnection();
    }
}





//@Override
//	public boolean delete(Object object) {
//		// TODO Auto-generated method stub
//		return false;
//	}
//	
//	@Override
//	public boolean update(String key, Object value, String column,Object columnvalue) {
//		boolean result = false;
//		String sql="update tbbanners set "+key+"=(?)"+" where "+column+"=(?)";
//
//		int count=0;
//		try {
//            pstmt=conn.prepareStatement(sql);
//           
//            pstmt.setObject(1, value);
//            pstmt.setObject(2, columnvalue);
//           
//            count=pstmt.executeUpdate();
//            if(count>0){
//                result=true;
//            }else{
//                result=false;
//            }
//        }catch(SQLException e){
//            e.printStackTrace();
//        }finally{
//        	dbconn.closeAll(rs, stmt, pstmt, conn);
//        }
//		return result;
//	}
//	//,,,,
//	@Override
//	public boolean updateAll(Object bean) {
//		boolean result = false;
//		Bean_Banner banner=(Bean_Banner)bean;
//		String sql="update tbbanners set title=(?),subtitle=(?),imageurl=(?),redirecturl=(?),status=(?),operatedate=(?) where id=(?)";
//		
//		int count=0;
//        try {
//            pstmt=conn.prepareStatement(sql);
//           
//            pstmt.setObject(1, banner.getTitle());
//            pstmt.setObject(2, banner.getSubtitle());
//            pstmt.setObject(3, banner.getImageurl());
//            pstmt.setObject(4, banner.getRedirecturl());
//            pstmt.setObject(5, banner.getStatus());
//            pstmt.setObject(6, banner.getOperatedate());
//            pstmt.setObject(7, banner.getId());
//            count=pstmt.executeUpdate();
//            if(count>0){
//                result=true;
//            }else{
//                result=false;
//            }
//        }catch(SQLException e){
//            e.printStackTrace();
//        }finally{
//        	dbconn.closeAll(rs, stmt, pstmt, conn);
//        }
//        return result;
//	}
//
//	@Override
//	public int totleRecords(String key, Object value) {
//		// TODO Auto-generated method stub
//		return 0;
//	}
//
//	@Override
//	public Object find(String key, Object value) {
//		Bean_Banner banner=null;
//		String sql="select * from tbbanners where "+key+"=(?)";
//		try {
//	        pstmt=conn.prepareStatement(sql);
//	        pstmt.setObject(1, value);
//	        rs=pstmt.executeQuery();
//	        
//	        while(rs.next()){
//	        	banner=new Bean_Banner();
//	        	banner.setId(rs.getInt("id"));
//	        	banner.setTitle(rs.getString("title"));
//	        	banner.setSubtitle(rs.getString("subtitle"));
//	        	banner.setImageurl(rs.getString("imageurl"));
//	        	banner.setRedirecturl(rs.getString("redirecturl"));
//	        	banner.setStatus(rs.getString("status"));
//	        	banner.setOperatedate(rs.getString("operatedate"));
//	        }
//		} catch (SQLException e) {
//	            e.printStackTrace();
//		}finally{
//			dbconn.closeAll(rs, stmt, pstmt, conn);
//		}
//		return banner;
//	}
//
//	@Override
//	public Object findWithAnd(String key1, Object value1, String key2,
//			Object value2) {
//		// TODO Auto-generated method stub
//		return null;
//	}
//
//	@Override
//	public Object findWithAnd(String key1, Object value1, String key2,
//			Object value2, String key3, Object value3) {
//		// TODO Auto-generated method stub
//		return null;
//	}
//
//	@Override
//	public List<Object> findAll() {
//		List<Object> list=new ArrayList<Object>();
//		String sql="select * from tbbanners order by id desc";
//		try {
//	        stmt=conn.createStatement();
//	        rs=stmt.executeQuery(sql);
//	        
//	        while(rs.next()){
//	        	Bean_Banner banner=new Bean_Banner();
//	        	banner.setId(rs.getInt("id"));
//	        	banner.setTitle(rs.getString("title"));
//	        	banner.setSubtitle(rs.getString("subtitle"));
//	        	banner.setImageurl(rs.getString("imageurl"));
//	        	banner.setRedirecturl(rs.getString("redirecturl"));
//	        	banner.setStatus(rs.getString("status"));
//	        	banner.setOperatedate(rs.getString("operatedate"));
//	            list.add(banner);
//	        }
//		} catch (SQLException e) {
//	            e.printStackTrace();
//		}finally{
//			dbconn.closeAll(rs, stmt, pstmt, conn);
//		}
//		return list;
//	}
//
//	@Override
//	public List<Object> findAll(String key, Object value) {
//		List<Object> list=new ArrayList<Object>();
//		String sql="select * from tbbanners where "+key+"=(?) order by id desc";
//		try {
//	        pstmt=conn.prepareStatement(sql);
//	        pstmt.setObject(1, value);
//	        rs=pstmt.executeQuery();
//	        
//	        while(rs.next()){
//	        	Bean_Banner banner=new Bean_Banner();
//	        	banner.setId(rs.getInt("id"));
//	        	banner.setTitle(rs.getString("title"));
//	        	banner.setSubtitle(rs.getString("subtitle"));
//	        	banner.setImageurl(rs.getString("imageurl"));
//	        	banner.setRedirecturl(rs.getString("redirecturl"));
//	        	banner.setStatus(rs.getString("status"));
//	        	banner.setOperatedate(rs.getString("operatedate"));
//	            list.add(banner);
//	        }
//		} catch (SQLException e) {
//	            e.printStackTrace();
//		}finally{
//			dbconn.closeAll(rs, stmt, pstmt, conn);
//		}
//		return list;
//	}
//
//	@Override
//	public Object Page(int curPage, int rowsPerPage) {
//		Bean_Page p=null;
//		
//	    int maxPage=0; //һ���ж���ҳ
//	    int maxRowCount=0; //һ���ж�����
//	    
//	    try {
//	    	stmt=conn.createStatement();
//			String SQL="select count(*) from tbbanners";
//			rs=stmt.executeQuery(SQL);
//			if(rs.next()){
//				maxRowCount=rs.getInt(1);
//				//����������������ҳ��
//				if(maxRowCount%rowsPerPage==0){
//	            	maxPage=maxRowCount/rowsPerPage;
//	        	}else{
//	            	maxPage=maxRowCount/rowsPerPage+1;
//	        	}
//			}
//			stmt2=conn.createStatement();
//        	String sql="select * from tbbanners order by id desc limit "+(curPage-1)*rowsPerPage+","+rowsPerPage;
//        	rs2=stmt2.executeQuery(sql);
//        	List<Object> list=new ArrayList<Object>();
//        	while(rs2.next()){
//        		Bean_Banner banner=new Bean_Banner();
//        		banner.setId(rs2.getInt("id"));
//	        	banner.setTitle(rs2.getString("title"));
//	        	banner.setSubtitle(rs2.getString("subtitle"));
//	        	banner.setImageurl(rs2.getString("imageurl"));
//	        	banner.setRedirecturl(rs2.getString("redirecturl"));
//	        	banner.setStatus(rs2.getString("status"));
//	        	banner.setOperatedate(rs2.getString("operatedate"));
//	            list.add(banner);
//        	}
//        	p=new Bean_Page();
//        	p.setMaxRowCount(maxRowCount);
//        	p.setMaxPage(maxPage);
//        	p.setRowsPerPage(rowsPerPage);
//        	p.setList(list);
//		} catch (SQLException e) {
//	            e.printStackTrace();
//		}finally{
//	        dbconn.close(rs);
//	        dbconn.close(rs2);
//	        dbconn.close(stmt);
//	        dbconn.close(stmt2);
//	        dbconn.close(conn);
//		}
//		return p;
//	}
//	
//	public Object PageBy(int curPage, int rowsPerPage,String key,Object value) {
//		Bean_Page p=null;
//		
//	    int maxPage=0; //һ���ж���ҳ
//	    int maxRowCount=0; //һ���ж�����
//	    
//	    try {
//	    	
//			String SQL="select count(*) from tbbanners where "+key+"=(?)";
//			pstmt=conn.prepareStatement(SQL);
//			pstmt.setObject(1, value);
//			rs=pstmt.executeQuery();
//			
//			if(rs.next()){
//				maxRowCount=rs.getInt(1);
//				//����������������ҳ��
//				if(maxRowCount%rowsPerPage==0){
//	            	maxPage=maxRowCount/rowsPerPage;
//	        	}else{
//	            	maxPage=maxRowCount/rowsPerPage+1;
//	        	}
//			}
//			
//        	String sql="select * from tbbanners where "+key+"=(?) order by id desc limit "+(curPage-1)*rowsPerPage+","+rowsPerPage;
//        	pstmt2=conn.prepareStatement(sql);
//        	pstmt2.setObject(1, value);
//        	rs2=pstmt2.executeQuery();
//        	List<Object> list=new ArrayList<Object>();
//        	while(rs2.next()){
//        		Bean_Banner banner=new Bean_Banner();
//        		banner.setId(rs2.getInt("id"));
//	        	banner.setTitle(rs2.getString("title"));
//	        	banner.setSubtitle(rs2.getString("subtitle"));
//	        	banner.setImageurl(rs2.getString("imageurl"));
//	        	banner.setRedirecturl(rs2.getString("redirecturl"));
//	        	banner.setStatus(rs2.getString("status"));
//	        	banner.setOperatedate(rs2.getString("operatedate"));
//	            list.add(banner);
//        	}
//        	p=new Bean_Page();
//        	p.setMaxRowCount(maxRowCount);
//        	p.setMaxPage(maxPage);
//        	p.setRowsPerPage(rowsPerPage);
//        	p.setList(list);
//		} catch (SQLException e) {
//	            e.printStackTrace();
//		}finally{
//	        dbconn.close(rs);
//	        dbconn.close(rs2);
//	        dbconn.close(pstmt);
//	        dbconn.close(pstmt2);
//	        dbconn.close(conn);
//		}
//		return p;
//	}